package com.gxa.modules.property.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.AssertUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.property.dto.NoticeToAdd;
import com.gxa.modules.property.dto.NoticeToUpdate;
import com.gxa.modules.property.entity.Notice;
import com.gxa.modules.property.entity.Urgency;
import com.gxa.modules.property.service.NoticeService;
import com.gxa.modules.property.service.UrgencyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "公告管理接口")
@RestController
@Slf4j
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private UrgencyService urgencyService;

    @ApiOperation(value = "公告添加接口")
    @PostMapping("/notice/add")
    public Result addNotice(@RequestBody NoticeToAdd noticeToAdd){
        if (noticeToAdd!= null){
            this.noticeService.add(noticeToAdd);
            return new Result().ok();
        }else {
            return new Result().error();
        }
    }


    @ApiOperation(value = "公告删除接口")
    @DeleteMapping("/notice/delete")
    public Result deleteNotice(@RequestParam Integer id){
        this.noticeService.delete(id);
        return new Result().ok();
    }

    @ApiOperation(value = "公告编辑接口")
    @PutMapping("/notice/update")
    public Result updateNotice(@RequestBody NoticeToUpdate noticeToUpdate){
        if (noticeToUpdate!=null){
            this.noticeService.update(noticeToUpdate);
            return new Result().ok();
        }else{
            return new Result().error();
        }
    }

    @ApiOperation(value = "公告查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前是第几页",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少条",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "noticeTitle",value = "公告标题",dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "communityId",value = "小区编号",dataType = "int")
    })
    @GetMapping("/notice/list")
    public com.gxa.modules.property.utils.Result<List<Notice>> queryNotice(@RequestParam Map<String,Object> params){
        log.info("----params----{}----",params);
        AssertUtils.isBlank(String.valueOf(params),"字符串不能为空");
        com.gxa.modules.property.utils.Result<List<Notice>> r = new com.gxa.modules.property.utils.Result<>();
        try {
            PageInfo<Notice> pageInfo = noticeService.queryNotice(params);
            r = com.gxa.modules.property.utils.Result.success(pageInfo.getList(),pageInfo.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            com.gxa.modules.property.utils.Result.failed();
        }
        return  r;
    }

    @ApiOperation(value = "公告的紧急程度的下拉列表")
    @GetMapping("/notice/urgencyList")
    public List<Urgency> getList(){
        return urgencyService.list();
    }

    @ApiOperation(value = "公告的批量删除")
    @DeleteMapping("/notice/deleteNoticeByIds")
    public Result removeNoticeByIds(@RequestParam List<Integer> ids){
         noticeService.deleteNoticeByIds(ids);
         return new Result().ok();
    }

}
